
# 需求学生管理系统
## 支持如下功能
"""
1. 支持登录，只有登录成功才能进行其它操作，(提示：登录函数实现)
2. 支持对学生信息进行增删改查。(提示:通过类实现，其中数据为数据库中学习的学生表Students数据导出写入到本地)
3. 要求返回的格式是字典  ：{'code':0,'message':'登录成功'}
4. 要求在登录成功时，加上登录时间，操作时间作为键值对放在返回的字典中
5. 做增删改查操作必须前提是先登录成功，若登录失败，让它继续登录 。
6. 必须是通过用户输入(控制台)
"""

records = [
    {
        "studentNo": 1,
        "name": "嬴政",
        "age": 52,
        "sex": "男",
        "class": "2班",
        "card": "1000200030004000",
        "city": "上海"
    },
    {
        "studentNo": 2,
        "name": "韩非子",
        "age": 29,
        "sex": "女",
        "class": "3班",
        "card": "1000200030003000",
        "city": "天津"
    },
    {
        "studentNo": 3,
        "name": "王剪",
        "age": 27,
        "sex": "女",
        "class": "4班",
        "card": "1000200030002000",
        "city": "河南"
    },
    {
        "studentNo": 4,
        "name": "刘邦",
        "age": 53,
        "sex": "男",
        "class": "5班",
        "card": "1000200030004000",
        "city": "北京"
    },
    {
        "studentNo": 5,
        "name": "韩信",
        "age": 32,
        "sex": "男",
        "class": "1班",
        "card": None,
        "city": "河北"
    },
    {
        "studentNo": 6,
        "name": "项宇",
        "age": 30,
        "sex": "女",
        "class": "5班",
        "card": "1000200030005000",
        "city": "天津"
    },
    {
        "studentNo": 7,
        "name": "李红广",
        "age": 40,
        "sex": "男",
        "class": "5班",
        "card": "1000200030008000",
        "city": "上海"
    },
    {
        "studentNo": 8,
        "name": "刘彻",
        "age": 39,
        "sex": "男",
        "class": "2班",
        "card": "1000200030007000",
        "city": "广州"
    },
    {
        "studentNo": 9,
        "name": "韩王",
        "age": 28,
        "sex": "女",
        "class": "6班",
        "card": None,
        "city": "上海"
    },
    {
        "studentNo": 10,
        "name": "韩大夫",
        "age": 21,
        "sex": "女",
        "class": "7班",
        "card": "1000200030009000",
        "city": "山东"
    },
    {
        "studentNo": 11,
        "name": "霍去病",
        "age": 29,
        "sex": "男",
        "class": "8班",
        "card": "1000200020004000",
        "city": "广州"
    },
    {
        "studentNo": 12,
        "name": "萧何",
        "age": 36,
        "sex": "男",
        "class": "3班",
        "card": "1000200030006000",
        "city": "上海"
    },
    {
        "studentNo": 13,
        "name": "卫青",
        "age": 37,
        "sex": "女",
        "class": "8班",
        "card": "1000200060004000",
        "city": "重庆"
    },
    {
        "studentNo": 14,
        "name": "曹总",
        "age": 26,
        "sex": "女",
        "class": "3班",
        "card": "1000200080004000",
        "city": "四川"
    },
    {
        "studentNo": 15,
        "name": "王韩亮",
        "age": 56,
        "sex": "女",
        "class": "2班",
        "card": None,
        "city": "云南"
    },
    {
        "studentNo": 16,
        "name": "刘备",
        "age": 51,
        "sex": "男",
        "class": "5班",
        "card": "1000100030004000",
        "city": "北京"
    },
    {
        "studentNo": 17,
        "name": "薛刚",
        "age": 28,
        "sex": "男",
        "class": "4班",
        "card": "1000100030005000",
        "city": "河南"
    },
    {
        "studentNo": 18,
        "name": "柳红",
        "age": 20,
        "sex": "男",
        "class": "4班",
        "card": "1000100030005000",
        "city": "广州"
    },
    {
        "studentNo": 19,
        "name": "陈胜",
        "age": 25,
        "sex": "男",
        "class": "1班",
        "card": "1000100030006000",
        "city": "北京"
    },
    {
        "studentNo": 20,
        "name": "刘韩",
        "age": 29,
        "sex": "男",
        "class": "1班",
        "card": "1000100030007000",
        "city": "上海"
    },
    {
        "studentNo": 21,
        "name": "霍去病",
        "age": 40,
        "sex": "男",
        "class": "2班",
        "card": "1000200030005010",
        "city": "海南"
    },
    {
        "studentNo": 22,
        "name": "项宇",
        "age": 20,
        "sex": "女",
        "class": "2班",
        "card": "1000200030006000",
        "city": "天津"
    },
    {
        "studentNo": 23,
        "name": "八戒",
        "age": 25,
        "sex": "男",
        "class": "2班",
        "card": "100085",
        "city": "上海"
    },
    {
        "studentNo": 24,
        "name": "韩正",
        "age": 90,
        "sex": "男",
        "class": "3班",
        "card": "1000200030005011",
        "city": "哈尔滨"
    },
    {
        "studentNo": 25,
        "name": "孙悟空",
        "age": 39,
        "sex": "男",
        "class": "6班",
        "card": "1010102323343",
        "city": "北京"
    },
    {
        "studentNo": 27,
        "name": "李四",
        "age": 29,
        "sex": "女",
        "class": "2班",
        "card": "4356789098765456",
        "city": "海南"
    },
    {
        "studentNo": 28,
        "name": "唐盛",
        "age": 53,
        "sex": "男",
        "class": "2班",
        "card": "5623782323",
        "city": "上海"
    },
    {
        "studentNo": 26,
        "name": "唐盛",
        "age": 53,
        "sex": "男",
        "class": "2班",
        "card": "5623782323",
        "city": "上海"
    }
]


def login(username,password):
    # 定义一个数据库中用户名
    db_username = 'admin'
    db_password = '123456'

    # 用户名或密码为空的情况，给出提示
    if username is None or username == "":
        return {"code":2,"message":"用户名不能为空!"}
    if password is None or password == "":
        return {"code":3,"message":"密码不能为空!"}

    # 用户输入的值和数据库的值是否匹配
    if username == db_username and password == db_password:
        return {"code":1,"message":"登录成功"}

    # 匹配失败的情况
    return {"code":0,"message":"登录失败"}


class Student:

    # 新增学生
    def add_student(self,studentNo,name,**kwargs):
        # 定义一个字典
        student_dict = {}

        # 1. 判断必填项
        if studentNo == "":
            return {"code":11,"message":"学生编号不能为空"}
        if name == "":
            return {"code":12,"message":"学生姓名不能为空"}

        # 2. 判断可选项的值是否符合业务逻辑
        age = kwargs.get('age')
        if not str(age).isdigit():
            return {"code":13,"message":"输入年龄类型不正确,要求是整数"}
        if not 0 < int(age) <= 120:
            return {"code":14,"message":"输入的年龄不符合要求"}
        # 判断学生性别只包含男和女
        # 判断身份证号必须是18位 。

        # 3. 添加学生信息
        student_dict['studentNo'] = studentNo
        student_dict['name'] = name
        student_dict.update(**kwargs)
        records.append(student_dict)
        return {"code":10,"message":"添加学生成功!","records":records}


    # 修改学生
    def edit_student(self,studentNo,**kwargs):
        # 判断输入编号是否正确
        if not str(studentNo).isdigit():
            return {"code":21,"message":"输入学生编号不符合要求"}
        if studentNo == "":
            return {"code":22,"message":"学生编号不能为空"}

        # 判断输入的编号是否在列表中
        for x in records:
            if x.get('studentNo') == int(studentNo):
                x.update(**kwargs)
                return {"code":20,"message":"编辑成功!","records":records}

        # 若没有找到，给出提示
        return {"code":23,"message":"没有找到所要编辑的数据"}




    # 查询学生
    def search_student(self,name):
        # 用户是否输入名字
        if name == "":
            return {"code":40,"message":"查询学生成功","records":records}

        # 用户输入名字
        lst = []
        for x in records:
            db_name = x.get('name')
            if name in db_name:
                lst.append(x)

        # 判断列表是否为空
        if lst:
            return {"code":40,"message":"查询学生成功","records":lst}

        return {"code":41,"message":"未查询到任何学生记录"}


    # 删除学生
    def delete_student(self,studentNo):
        # 判断输入编号是否正确
        if not str(studentNo).isdigit():
            return {"code": 31, "message": "输入学生编号不符合要求"}
        if studentNo == "":
            return {"code": 32, "message": "学生编号不能为空"}

        # 循环学生
        studentNo = int(studentNo)      # 用户传入
        for x in records:
            sid = x.get('studentNo')        # 从数据库中获取
            if sid == studentNo:
                records.remove(x)
                return {"code":30,"message":"删除成功","records":records}

        return {"code":33,"message":"没有找到所要删除的记录"}


if __name__ == '__main__':
    result = None
    s = Student()

    # 进行循环
    flag = True
    while flag:
        vls = input("请输入对应编号进行操作:"
              "\n l:)进行登录"
              "\n i:)新增学生信息"
              "\n u:)修改学生信息"
              "\n d:)删除学生信息"
              "\n s:)查询学生信息"
              "\n q:) 退出操作")

        # 判断用户输入支付
        if not vls in ('l','q','i','u','d','s'):
            continue

        # 进行登录
        if vls == 'l':
            username = input("请输入用户名:")
            password = input("请输入密码:")
            result = login(username,password)
            print(result)

        # 新增学生
        if vls == 'i':
            # 判断是否登录
            if result.get('code') != 1:
                print({"code":4,"message":"用户未登录,请先进行登录操作"})
                continue
            student_no = input("请输入学生编号:")
            name = input("请输入学生姓名:")
            age = input("请输入学生年龄:")
            sex = input("请输入学生性别:")
            s_class = input("请输入学生班级:")
            card = input("请输入学生身份证号:")
            city = input("请输入学生城市:")

            s_dict = {"age":age,"name":name,"sex":sex,"class":s_class,"card":card,"city":city}
            # 调用新增方法
            add_result = s.add_student(student_no,**s_dict)
            print(add_result)

        if vls == 'u':
            pass

        if vls == 's':
            pass

        if vls == 'd':
            pass

        if vls == 'q':
            flag = False